//
//  SDSimpleImageView.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDSimpleImageView.h"
#import "SDImageCacheEx.h"

@implementation SDSimpleImageView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.contentMode = UIViewContentModeScaleAspectFill;
        //self.clipsToBounds = YES;
        self.userInteractionEnabled = YES;

    }
    
    return self;
}

- (void) setDefaultImage:(UIImage *)defaultImage
{
    //_defaultImage = [self modifiedImageWithImage:defaultImage];
    _defaultImage = defaultImage;
}

- (void)setImageWithURL:(NSString *)url
{
    if(!CHECK_VALID_STRING(url))
    {
        [self clearImage];
        return;
    }
    
    if ([self.url isEqualToString:url])
    {
        return;
    }
    else
    {
        [self clearImage];
    }
    
    self.url = url;
    
    __weak SDSimpleImageView* weakSelf = self;
    [[SDImageCacheEx sharedInstance]getImageWithURL:self.url
                                       identifier:[NSString stringWithFormat:@"com.tencent.utopia.simpleImageView.%lld",(long long)self]
                                      modifyBlock:^UIImage *(UIImage *image) {
                                          return [weakSelf modifiedImageWithImage:image];
                                      }
                                 modifyIdentifier:[self modifiedIndentifier]
                                       completion:^(UIImage *image) {
                                           if(image)
                                           {
                                               [weakSelf setImage:image];
                                               if( weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(simpleImageViewDidFinishLoading:)] )
                                                   [weakSelf.delegate simpleImageViewDidFinishLoading:weakSelf];
                                           }
                                           else
                                           {
                                               [weakSelf setImage:weakSelf.defaultImage];
                                           }
                                       }];
}

- (void)clearImage
{
    [self setImage:self.defaultImage];
    [[SDImageCacheEx sharedInstance]stopGettingImage:self.url identifier:[NSString stringWithFormat:@"com.tencent.simpleImageView.%lld",(long long)self]];
    self.url = nil;
}


- (void)dealloc
{
    [self clearImage];
}

- (UIImage*)modifiedImageWithImage:(UIImage*)image
{
    return image;
}

- (NSString*)modifiedIndentifier
{
    return nil;
}

@end

@implementation SDAvatarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.contentMode = UIViewContentModeScaleAspectFill;
    }
    return self;
}


- (UIImage*)modifiedImageWithImage:(UIImage*)image
{
    CGSize size = image.size;
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    CGFloat radius = ceilf(MIN(size.width, size.height)/2);
    CGRect frame = CGRectMake(size.width/2-radius, size.height/2-radius, radius*2, radius*2);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, YES);
    CGContextSetAllowsAntialiasing(context,YES);
    
    CGContextAddEllipseInRect(context, CGRectMake(frame.origin.x+1, frame.origin.y+1, frame.size.width-2, frame.size.height-2));
    CGContextClip(context);
    
    CGFloat scaleX = frame.size.width/size.width;
    CGFloat scaleY = frame.size.height/size.height;
    CGFloat scale = MAX(scaleX, scaleY);
    CGSize newsize = CGSizeMake(size.width*scale, size.height*scale);
    
    [image drawInRect:CGRectMake((frame.size.width-newsize.width)/2, 0, newsize.width, newsize.height)];
    
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (NSString*)modifiedIndentifier
{
    return @"com.tencent.avatar";
}

@end

//#import "SDFrostedGlass.h"
//
//@implementation SDMosaicsView
//
//- (id)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self)
//    {
//        self.contentMode = UIViewContentModeScaleAspectFill;
//        self.clipsToBounds = YES;
//    }
//    return self;
//}
//
//- (UIImage*)modifiedImageWithImage:(UIImage*)image
//{
//    image = [SDFrostedGlass blurWithCoreImage:image withFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
//    return image;
//}
//
//- (NSString*)modifiedIndentifier
//{
//    return @"com.tencent.utopia.Mosaics";
//}
//
//@end
